/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.CustomerIOTRPortal;

import session.TX.GroupBuyCustomerSBRemote;
import entity.GroupBuyingItem;
import Web_Helper.Helper_PaymentValidator;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean (name = "groupBuyCustManagedBean")
@SessionScoped
public class GroupBuyCustMB {
    @EJB 
    GroupBuyCustomerSBRemote gbManagerBean;
    
    GroupBuyingItem selectedItem;
    Integer numberToBuy = 0 ;
    String customerEmail;
    Long customerId;
    /** Creates a new instance of GroupBuyCustMB */
    public GroupBuyCustMB() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        customerId = (Long)session.getAttribute("Sid");
    }
    
    public List<GroupBuyingItem> getAvailableItems(){
        return gbManagerBean.getAvailableGBItem();
    }
    
    public Integer calculateDiscount(GroupBuyingItem item){
        if (item == null) return null;
        return new Double((item.getOriginalPrice()-item.getOfferedPrice())*100/
                item.getOriginalPrice()).intValue();
    }

    public GroupBuyingItem getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(GroupBuyingItem selectedItem) {
        this.selectedItem = selectedItem;
    }

    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }
    
    public Integer getNumberToBuy() {
        return numberToBuy;
    }

    public void setNumberToBuy(Integer numberToBuy) {
        this.numberToBuy = numberToBuy;
    }
    
    public void handleNumberToBuyChange (){
        
    }
    
    public List<Integer> computeQuotaSeq(){
        return gbManagerBean.getQuotaSeq(selectedItem, customerId); 
    }
    
    public Double getSubtotal(){
        if (selectedItem == null || numberToBuy == null){
            System.out.println(selectedItem);
            System.out.println(numberToBuy);
            return new Double(0);
        }
        return selectedItem.getOfferedPrice()*numberToBuy;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }
    
    //Perform validation of a payment
    public void validatePayment() {

        boolean validationResult = false;
        try {
            validationResult = Helper_PaymentValidator.validatePayment();
        } catch (IOException e) {
        }

        if (validationResult == true) {
            System.out.println("Payment Validation: SUCCESS");


            //this.doTransactionLog();

        }//end THEN block
        else {
            System.out.println("Payment Validation: FAIL");
        }

    }//------------------------------------------------------------------------------------------
    
    
    
    
    public void doTransactionLog(){
    }
    
    
    
    
}